package edu.xust.space.controller;

import edu.xust.space.mapper.CmarticleMapper;
import edu.xust.space.mapper.ProductMapper;
import edu.xust.space.pojo.Catalog;
import edu.xust.space.pojo.Cmarticle;
import edu.xust.space.pojo.ProductParamValue;
import edu.xust.space.pojo.Productinfo;
import edu.xust.space.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.List;

@Controller
public class ProductController {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CmarticleMapper cmarticleMapper;
    @Autowired
    ProductService productService;
    //根据父id查询
    @RequestMapping("/getproductbyparentid")
    @ResponseBody
    public List<Catalog> getProductByParentId(String parentId){
        List<Catalog> catalogByParentId = productMapper.getCatalogByParentId("05c9a0f7-d25b-47ff-a3f7-5c74b9332502");
        return catalogByParentId;
    }
    //根据id查询产品详情
    @RequestMapping("/getproductbyparentname")
    @ResponseBody
    public List<Catalog> getProductByParentName(@PathParam("parentName") String parentName){
        List<Catalog> product = productMapper.getCatalogByParentName(parentName);
        return product;
    }
    //根据名字查询产品详情
    @RequestMapping("/getproductdetilbyname")
    public String   getDetilProductByName(@PathParam("cName") String cName, Model model){
        List<Productinfo> productDetil = productMapper.getProductDetilByName(cName);
        model.addAttribute("infos",productDetil);
        return "product";
    }
    //获得一个产品
    @RequestMapping("/getranoneproduct")
    public String getOneProduct(Model model){
        //获得一个产品
        Productinfo ranOneProduct = productService.getRanOneProduct();
        //行业资讯相关信息
        List<Cmarticle> allCmarticle = cmarticleMapper.getAllCmarticle("b2fa8194-1c71-4629-ae09-b9a0ef68ba55");//行业自资讯
        model.addAttribute("product",ranOneProduct);
        model.addAttribute("articles",allCmarticle);
        return "index";
    }
    //根据产品id查询获取产品参数
    @RequestMapping("/getparam")
    public String getParam(Model model,@PathParam("id") String id){
        //产品参数信息
        List<ProductParamValue> ranOneProductParamByid = productService.getRanOneProductParamByid(id);
        // 产品信息
        Productinfo productDetilById = productMapper.getProductDetilById(id);
        model.addAttribute("params",ranOneProductParamByid);
        model.addAttribute("product",productDetilById);
        return "detail";
    }
    //比较两个产品
    @RequestMapping("/compare")
    public  String getMoreDetilProductById(Model model, @PathParam("compare") String[] compare){
//       第一个产品信息和参数
        Productinfo productDetil0 = productMapper.getProductDetilById(compare[0]);
       List<ProductParamValue> param0 = productMapper.getRanOneProductParamByid(compare[0]);
        //       第二个产品信息和参数
       Productinfo productDetil1 = productMapper.getProductDetilById(compare[1]);
       List<ProductParamValue> param1 = productMapper.getRanOneProductParamByid(compare[1]);

        model.addAttribute("params0",param0);
        model.addAttribute("params1",param1);
        model.addAttribute("product0",productDetil0);
        model.addAttribute("product1",productDetil1);
        return "compare";
    }
}
